/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */
import { describe, expect, test } from '@jest/globals';
import fs from 'fs';
import path from 'path';

import { initByStageEmptyApp, initExecuteCommand, runHvigor } from '../common/common';
import { Probes } from '../common/probes';

describe('hot reload mock', () => {

  test.concurrent('stage esmodule app', async () => {
    await initByStageEmptyApp(async (dir) => {
      // pre execution
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'entry',
        tasks: 'buildHotReloadResource'
      });
      const buildResResult = runHvigor(dir, commands);
      expect(buildResResult.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();

      // webpack watching..

      // post execution
      const packHotReloadHap = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'entry',
        tasks: 'assembleHap',
        hotReload: true
      });

      const packHotReload = runHvigor(dir, packHotReloadHap);
      expect(packHotReload.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();

      const probe = new Probes(dir, path.resolve(dir, 'entry'));
      expect(fs.readdirSync(probe.getIntermediatesLoaderOutPath())).toContain('js');
      expect(fs.readdirSync(`${probe.getIntermediatesLoaderOutPath()}/js`)).toContain('widget');
    });
  });

});
